{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%loadFromPOM\n",
    "<dependency>\n",
    "    <groupId>tech.tablesaw</groupId>\n",
    "    <artifactId>tablesaw-jsplot</artifactId>\n",
    "    <version>0.38.1</version>\n",
    "</dependency>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// For rendering plots in Jupyter Notebook\n",
    "import io.github.spencerpark.ijava.IJava;\n",
    "\n",
    "IJava.getKernelInstance().getRenderer()\n",
    "    .createRegistration(tech.tablesaw.plotly.components.Figure.class)\n",
    "    .preferring(io.github.spencerpark.jupyter.kernel.display.mime.MIMEType.TEXT_HTML)\n",
    "    .register((figure, ctx) -> {\n",
    "        ctx.renderIfRequested(io.github.spencerpark.jupyter.kernel.display.mime.MIMEType.TEXT_HTML, () -> {\n",
    "            String id = UUID.randomUUID().toString().replace(\"-\", \"\");\n",
    "            boolean plotImage = System.getenv().containsKey(\"D2L_PLOT_IMAGE\");\n",
    "\n",
    "            figure.asJavascript(id);\n",
    "            Map<String, Object> context = figure.getContext();\n",
    "            StringBuilder html = new StringBuilder();\n",
    "            html.append(\"<img id=\\\"\").append(id).append(\"_img\\\"></img>\\n\");\n",
    "            html.append(\"<div id=\\\"\").append(id).append(\"\\\"></div>\\n\");\n",
    "            html.append(\"<script>require(['https://cdn.plot.ly/plotly-1.57.0.min.js'], Plotly => {\\n\");\n",
    "            if (!plotImage) {\n",
    "                // render javascript\n",
    "                html.append(\"var \").append(context.get(\"targetName\")).append(\" = document.getElementById('\").append(id).append(\"');\\n\");\n",
    "            } else {\n",
    "                // render image\n",
    "                html.append(\"var \").append(context.get(\"targetName\")).append(\" = document.createElement('div');\\n\");\n",
    "                html.append(\"var \").append(context.get(\"targetName\") + \"_img\").append(\" = document.getElementById('\").append(id).append(\"_img');\\n\");\n",
    "                String plotFunction = (String) context.get(\"plotFunction\");\n",
    "                String imgExt = \".then(function(gd) { Plotly.toImage(gd).then(function(url) { \" + context.get(\"targetName\") + \"_img\" + \".src=url; } )});\";\n",
    "                context.put(\"plotFunction\", plotFunction.substring(0, plotFunction.length() - 1) + imgExt);\n",
    "            }\n",
    "            html.append(context.get(\"figure\")).append('\\n');\n",
    "            html.append(context.get(\"plotFunction\")).append('\\n');\n",
    "            html.append(\"})</script>\\n\");\n",
    "            return html.toString();\n",
    "    });\n",
    "});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tech.tablesaw.api.*;\n",
    "import tech.tablesaw.columns.*;\n",
    "import tech.tablesaw.plotly.*;\n",
    "import tech.tablesaw.plotly.api.*;\n",
    "import tech.tablesaw.plotly.components.*;\n",
    "import tech.tablesaw.plotly.traces.*;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "14.0.1+7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
